package com.fqxiny.lease.web.admin.controller.user;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.UpdateById;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fqxiny.lease.common.result.Result;
import com.fqxiny.lease.model.entity.BaseEntity;
import com.fqxiny.lease.model.entity.UserInfo;
import com.fqxiny.lease.model.entity.ViewAppointment;
import com.fqxiny.lease.model.enums.BaseStatus;
import com.fqxiny.lease.web.admin.service.UserInfoService;
import com.fqxiny.lease.web.admin.vo.user.UserInfoQueryVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "用户信息管理")
@RestController
@RequestMapping("/admin/user")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    @Operation(summary = "分页查询用户信息")
    @GetMapping("page")
    public Result<IPage<UserInfo>> pageUserInfo(@RequestParam long current, @RequestParam long size, UserInfoQueryVo queryVo) {
        Page<UserInfo> page = new Page<>(current, size);
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(queryVo.getPhone() != null,UserInfo::getPhone, queryVo.getPhone());
        wrapper.eq(queryVo.getStatus() != null,UserInfo::getStatus, BaseStatus.ENABLE);
        wrapper.eq(BaseEntity::getIsDeleted,0);
        Page<UserInfo> page1 = userInfoService.page(page, wrapper);
        return Result.ok(page1);
    }

    @Operation(summary = "根据用户id更新账号状态")
    @PostMapping("updateStatusById")
    public Result updateStatusById(@RequestParam Long id, @RequestParam BaseStatus status) {
        boolean update = userInfoService.update(
                Wrappers.lambdaUpdate(UserInfo.class)
                        .set(UserInfo::getStatus, status)
                        .eq(UserInfo::getId, id)
        );
        return update ? Result.ok() : Result.fail();
    }
}
